package dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import modle.Orders;


public class OrdersDao {
	
	//增
	public static int OrdersAdd(Connection con, int id) throws Exception{
		String sql = "insert into `Orders`(`Status`, `Customer`) values(?,?)";
//		INSERT 语句中，前后列数不等会造成："mysql错误:Column count doesn't match value count at row 1"
		PreparedStatement pstmt = con.prepareStatement(sql);
		pstmt.setInt(1, 0);
		pstmt.setInt(2, id);
		return pstmt.executeUpdate();
	}
	
	//改
	public static int OrdersUpdate(Connection con, Orders orders, int id) throws Exception{
		String sql = "update `orders` set Status = ? where Id = ?";
		PreparedStatement pstmt = con.prepareStatement(sql);
		pstmt.setInt(1, 1);
		pstmt.setInt(2, id);
		return pstmt.executeUpdate();
	}
	
	public static int OrdersUpdate_st(Connection con, int id, String s) throws Exception{
		String sql = "update `orders` set Status = ?, Feedback = ? where Id = ?";
		PreparedStatement pstmt = con.prepareStatement(sql);
		pstmt.setInt(1, 2);
		pstmt.setString(2, s);
		pstmt.setInt(3, id);
		return pstmt.executeUpdate();
	}

	//查
	public static Orders OrdersSearch(Connection con, int Id) throws Exception{
		
		Orders resultOrders = null;
		
		String sql = "select * from `orders` where `Id` = ?";
		PreparedStatement pstmt = con.prepareStatement(sql);
		pstmt.setInt(1, Id);
		ResultSet rs = pstmt.executeQuery();
		if(rs.next()) {
			resultOrders = new Orders();							//new 一个空间，要不就会是空指针变量访问
			resultOrders.setId(rs.getInt("Id"));
			resultOrders.setStatus(rs.getInt("Status"));
			resultOrders.setFeedback(rs.getString("Feedback"));
			resultOrders.setCustomer(rs.getInt("Customer"));
		}
		
		return resultOrders;
	}
	
	public static ResultSet OrdersSearch_set(Connection con, int Id) throws Exception{
		
		Orders resultOrders = null;
		
		String sql = "select * from `orders` where `Customer` = ?";
		PreparedStatement pstmt = con.prepareStatement(sql);
		pstmt.setInt(1, Id);
		ResultSet rs = pstmt.executeQuery();
		
		return rs;
	}
		
}
